'use strict'

var local = document.getElementById("local");
var remote = document.getElementById("remote");

var start_btn = document.getElementById("start");
var call_btn = document.getElementById("call");
var end_btn = document.getElementById("end");

start_btn.onclick = start;
call_btn.onclick = call;
end_btn.onclick = end;

//本地
var pc1;
//本地流
var localStream;
//远端
var pc2;

var mediaObj = {
	audio:false,
	video:true
}

var offerOptions = {
  offerToReceiveAudio: 1,
  offerToReceiveVideo: 1
};

function start(){
	console.log("start 被电击")
	navigator.mediaDevices.getUserMedia(mediaObj).then(
		function(stream){
			console.log(stream)
			local.srcObject = stream;
			console.log(local)
			localStream = stream;
		}
	).catch(function(e){
		console.log("getUserMedia发生错误："+e);
	});
}

function call(){
	//服务器
	var servers = null;
	pc1 = new RTCPeerConnection(servers);
	pc2 = new RTCPeerConnection(servers);
	//RTCPeerConnection.onicecandidate属性指定了一个事件处理函数
	//当icecandidate事件发生时被调用。
	pc1.onicecandidate = function(e){
		if(e.candidate){
			console.log("pc1 icecandidate事件 "+e)
			pc2.addIceCandidate(
				new RTCIceCandidate(e.candidate)
			).then(
				function(){
					console.log("远程pc2 成功添加pc1");
				},
				function(err){
					console.log("远程pc2 添加pc1 失败"+err);
				}
			);
		}
	};
	
	pc2.onicecandidate = function(e){
		if(e.candidate){
			console.log("pc2 icecandidate事件 "+e)
			pc1.addIceCandidate(
					new RTCIceCandidate(e.candidate)
			).then(
				function(){
					console.log("远程pc1 成功添加pc2");
				},
				function(err){
					console.log("远程pc1 添加pc2 失败"+err);
				}
			);
		}
	};
		
		pc1.oniceconnectionstatechange = function(e){
			console.log("pc1 iceConnectionState属性发生改变"+e)
		}
		
		pc2.oniceconnectionstatechange = function(e){
			console.log("pc2 iceConnectionState属性发生改变"+e)
		}
		
		pc2.onaddstream = function(e){
			remote.srcObject = e.stream;
			console.log('pc2 添加远程流'+e)
			console.log(remote.srcObject)
		};
		
		pc1.addStream(localStream);
		console.log("pc1 添加本地媒体流")
		
		pc1.createOffer(
			offerOptions
		).then(
			//创建成功
			function(desc){
				pc1.setLocalDescription(desc).then(
					//成功
					function(){
						console.log("pc1 设置本地描述符成功");
					},
					//失败
					function(){
						console.log("pc1 设置本地描述符失败");
					}
				);
				
				pc2.setRemoteDescription(desc).then({
					//成功
					function(){
						console.log("pc2 成功设置远程描述符");
					},
					//失败
					function(){
						console.log("pc2 设置远程描述符成功");
					}
				});
				
				pc2.createAnswer().then({
					//成功应答
					function(){
						pc2.setLocalDescription(desc).then({
							function(){
								console.log("pc2 设置本地描述符成功");
							},
							function(){
								console.log("pc2 设置本地描述符失败");
							}
						});
						
						pc1.setRemoteDescription(desc).then({
							//成功
							function(){
								console.log("pc1 成功设置远程描述符");
							},
							//失败
							function(){
								console.log("pc1 设置远程描述符失败");
							}
						});
					},
					//应答失败
					function(err){
						console.log("应答pc2失败")
					}
				});
			},
			//创建失败
			function(){
				console.log("offer创建失败")
			}
		);
}

function end(){
	pc1.close();
	console.log("关闭pc1 RTCPeerConnection");
	pc2.close();
	console.log("关闭pc2 RTCPeerConnection");
}
